// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Gunnar Ritter
//
// Unix specializations for localhost probe.

#include <time.h>
#include <sys/utsname.h>
#include <locale.h>

#include "probes/localhost.h"

namespace popdiag {

// retrieve the offset to be applied to GMT to get local time.
// return MAX_INT if you cannot determine it
int LocalHostProbe::GetOffsetFromGMTInMinutes() {
  time_t t;
  struct tm tm;

  if (time(&t) == static_cast<time_t>(-1))
    return INT_MAX;
  return (t - mktime(gmtime_r(&t, &tm))) / 60;
}

QString LocalHostProbe::GetTimezoneName() {
  time_t t;
  struct tm tm;
  char buf[256];

  if (time(&t) == (time_t)-1)
    return "Unknown";
  strftime(buf, sizeof buf, "%Z", localtime_r(&t, &tm));
  return buf;
}

void LocalHostProbe::LogLocale() {
  LogDetails("Locale", true, setlocale(LC_ALL, NULL));
}

void LocalHostProbe::LogOSVersion() {
  struct utsname name;

  if (uname(&name) != -1) {
    LogDetails("Operating System", true, name.sysname);
    LogDetails("Operating System Release", true, name.release);
    LogDetails("Operating System Version", true, name.version);
  } else {
    LogDetails("Operating System", false, "Unknown");
    LogDetails("Operating System Release", false, "Unknown");
    LogDetails("Operating System Version", false, "Unknown");
  }
}

}  // namespace popdiag
